import GoodsItem from '../components/goods-item.vue'
export default {
	components: {
		GoodsItem
	},
	data() {
		return {
			goods: {},
			goodsList: [],
			showGoods: false,
			goodsDetail: {},
			goodsId: '',
			active: 0,
			tempAllGoods: [],
			limit: 10,
			swiperGoodsList: [],
			showAnimate: false,
			gailvList: [],
			commentList: [],
			commentNum: 0,
			jwt: ''
		};
	},
	async onLoad(options) {
		this.goodsId = options.goodsId ? options.goodsId : ''


		this.jwt = this.$store.get('jwt')
		var share = this.$store.get('share')

		if (!this.jwt && share == 1) {
			this.$store.set('goodsId', this.goodsId)
			this.$store.set('page', 'manghe/detail/detail')
		}

		await this.getDetail()
		await this.getCommentList()
	},
	onShareAppMessage(res) {
		return {
			title: this.goods.name,
			path: `pages/manghe/detail/detail?goodsId=${this.goodsId}`,
			desc: "欢迎进入潮玩汇聚",
			content: "欢迎进入潮玩汇聚",
			imageUrl: this.goods.img,
			success(res) {
				uni.showToast({
					title: '分享成功'
				})
			},
			fail(res) {
				uni.showToast({
					title: '分享失败',
					icon: 'none'
				})
			}
		}
	},
	methods: {
		// 获取详情
		async getDetail() {
			var res = await this.$get('server/index/getmanghegoodsdetails', {
				id: this.goodsId
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.goods = res.goods
			this.tempAllGoods = res.list
			this.goodsList = this.getFilterGoods(0, res.list)
			var list = res.list

			this.swiperGoodsList = list

			this.showAnimate = true

			setTimeout(() => {
				this.showAnimate = false
			}, 1000)

			for (var i = 1; i < 5; i++) {
				var obj = {
					name: this.$getSignText(i),
					val: this.goods['gailv' + i],
					id: i
				}
				this.gailvList.push(obj)
			}
		},
		async getCommentList() {
			var res = await this.$get('server/index/getpingjia', {
				id: this.goodsId,
				type: 1,
				biaoqian: '',
				biaoqianid: '',
				page: 1,
				limit: 2
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.commentList = res.list
			this.commentNum = res.count
		},
		getColor(id) {
			if (id == 1) {
				return "#FF4C4C"
			} else if (id == 2) {
				return "#FDA11E"
			} else if (id == 3) {
				return "#785FF9"
			} else if (id == 4) {
				return "#156BF8"
			} else {
				return "#333"
			}
		},
		onGoodsChange() {
			this.showAnimate = true
			console.log('动作出现')

			setTimeout(() => {
				this.showAnimate = false
			}, 1000)
		},
		// 商品类型过滤
		getFilterGoods(type, list) {
			if (type == 0) return list;
			var newList = list.filter(item =>
				item.type == type
			)
			return newList;
		},
		// 切换保底商品
		onTabClick(item, index) {
			this.active = index
			this.goodsList = this.getFilterGoods(item.id, this.tempAllGoods)
		},
		// 购买盲盒
		async onBuy(num) {
			if (!this.jwt) {
				this.$toast('请先登录')
				setTimeout(() => {
					this.$reLaunchTo('pages/login/login')
				}, 1000)
				return
			}

			this.$navTo('pages/manghe/submit/submit', {
				goodsId: this.goodsId,
				num: num,
				goods: encodeURIComponent(JSON.stringify(this.goods))
			})
		},
		onDetail(item) {
			this.goodsDetail = Object.assign({}, item)
			this.$refs.detail.open()
			// this.goodsDetail.details.replace(/\<img/gi, '<img style="max-width:100% !import;height:auto" ')
		},
		// 新手试玩
		onTry() {
			this.$navTo('pages/manghe/try/try')
		},
		// 点击更多
		onMore() {
			uni.navigateBack()
		}
	}
};
